﻿using per.esmool.utils.DB;
using per.esmool.utils.DB.MySQL;
using System;

namespace FlowSimulate.MySQL {

    public class Task() : IIDData<Task> {

        public Task Copy() {
            var task = new Task() {
                Id             = this.Id,
                Parent         = this.Parent,
                Project        = this.Project,
                Module         = this.Module,
                Story          = this.Story,
                StoryVersion   = this.StoryVersion,
                FromBug        = this.FromBug,
                Name           = this.Name,
                Type           = this.Type,
                Pri            = this.Pri,
                Estimate       = this.Estimate,
                Consumed       = this.Consumed,
                Left           = this.Left,
                Deadline       = this.Deadline,
                Status         = this.Status,
                Color          = this.Color,
                Mailto         = this.Mailto,
                Desc           = this.Desc,
                OpenedBy       = this.OpenedBy,
                OpenedDate     = this.OpenedDate,
                AssignedTo     = this.AssignedTo,
                AssignedDate   = this.AssignedDate,
                EstStarted     = this.EstStarted,
                RealStarted    = this.RealStarted,
                FinishedBy     = this.FinishedBy,
                FinishedDate   = this.FinishedDate,
                FinishedList   = this.FinishedList,
                CanceledBy     = this.CanceledBy,
                CanceledDate   = this.CanceledDate,
                ClosedBy       = this.ClosedBy,
                ClosedDate     = this.ClosedDate,
                ClosedReason   = this.ClosedReason,
                LastEditedBy   = this.LastEditedBy,
                LastEditedDate = this.LastEditedDate,
                Deleted        = this.Deleted,
            };
            return task;
        }

        [DbField] public int Id { get; set; }
        [DbField] public int Parent { get; set; }
        [DbField] public int Project { get; set; }
        [DbField] public int Module { get; set; }
        [DbField] public int Story { get; set; }
        [DbField] public int StoryVersion { get; set; }
        [DbField] public int FromBug { get; set; }
        [DbField] public string Name { get; set; }
        [DbField] public string Type { get; set; }
        [DbField] public int Pri { get; set; }
        [DbField] public float Estimate { get; set; }
        [DbField] public float Consumed { get; set; }
        [DbField] public float Left { get; set; }
        [DbField] public DateTime Deadline { get; set; }
        [DbField] public string Status { get; set; }
        [DbField] public string Color { get; set; }
        [DbField] public string Mailto { get; set; }
        [DbField] public string Desc { get; set; }
        [DbField] public string OpenedBy { get; set; }
        [DbField] public DateTime OpenedDate { get; set; }
        [DbField] public string AssignedTo { get; set; }
        [DbField] public DateTime AssignedDate { get; set; }
        [DbField] public DateTime EstStarted { get; set; }
        [DbField] public DateTime RealStarted { get; set; }
        [DbField] public string FinishedBy { get; set; }
        [DbField] public DateTime FinishedDate { get; set; }
        [DbField] public string FinishedList { get; set; }
        [DbField] public string CanceledBy { get; set; }
        [DbField] public DateTime CanceledDate { get; set; }
        [DbField] public string ClosedBy { get; set; }
        [DbField] public DateTime ClosedDate { get; set; }
        [DbField] public string ClosedReason { get; set; }
        [DbField] public string LastEditedBy { get; set; }
        [DbField] public DateTime LastEditedDate { get; set; }
        [DbField] public bool Deleted { get; set; }
    }

    public class TaskDao() : MySQLModel<Task>() {

        public override string TableName => "zt_task";

        public int Insert(
            string title, string description,
            string assetOwner, string assetPM,
            DateTime openDate, DateTime? assignDate, DateTime? finishDate, DateTime? closeDate
        ) {
            var noDate = DateTime.Parse("0000-00-00");
            var task = new Task() {
                Parent         = 0                       , // <固定值>
                Project        = 1                       , // <固定值>
                Module         = 0                       , // <固定值>
                Story          = 0                       , // <固定值>
                StoryVersion   = 1                       , // <固定值>
                FromBug        = 0                       , // <固定值>
                Name           = title                   , // <任务名称>
                Type           = "devel"                 , // <固定值>
                Pri            = 3                       , // <固定值>
                Estimate       = 0                       , // <固定值>
                Consumed       = 3                       , // <Cost转换为工作小时>
                Left           = 0                       , // <固定值>
                Deadline       = noDate                  , // <固定值>
                Status         = "closed"                , // <固定值>
                Color          = ""                      , // <固定值>
                Mailto         = ""                      , // <固定值>
                Desc           = description             , // <任务描述html>
                OpenedBy       = assetOwner              , // <启动者>
                OpenedDate     = openDate                , // <启动时间>
                AssignedTo     = assetPM                 , // <固定值>
                AssignedDate   = assignDate ?? noDate    , // <关闭时间>
                EstStarted     = noDate                  , // <固定值>
                RealStarted    = noDate                  , // <固定值>
                FinishedBy     = assetOwner              , // <任务执行人>
                FinishedDate   = finishDate ?? noDate    , // <完成时间>
                FinishedList   = ""                      , // <固定值>
                CanceledBy     = ""                      , // <固定值>
                CanceledDate   = noDate                  , // <固定值>
                ClosedBy       = assetPM                 , // <关闭者PM>
                ClosedDate     = closeDate ?? noDate     , // <关闭时间>
                ClosedReason   = "done"                  , // <固定值>
                LastEditedBy   = assetPM                 , // <关闭者PM>
                LastEditedDate = closeDate ?? noDate     , // <关闭时间>
                Deleted        = false                   , // <固定值>
            };
            return this.Insert(task);
        }
    }
}
